﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDMS.CommonForm
{

    public class DocumentManagerEx
    {
        private DevExpress.XtraBars.Docking2010.Views.Tabbed.TabbedView tabbedView1;
        public DocumentManagerEx(DevExpress.XtraBars.Docking2010.Views.Tabbed.TabbedView tabbedView1)
        {
            foreach (DevExpress.XtraBars.Docking2010.Views.BaseDocument doc in tabbedView1.Documents)
            {
                tabbedView1.Controller.RemoveDocument(doc);
            }

            this.tabbedView1 = tabbedView1;
            this.tabbedView1.QueryControl += tabbedView1_QueryControl;
            tabbedView1.DocumentSelectorShown += tabbedView1_DocumentSelectorShown;
        }

        void tabbedView1_DocumentSelectorShown(object sender, EventArgs e)
        {

        }

        public bool ExistTab(DevExpress.XtraEditors.XtraUserControl control)
        {
            foreach (DevExpress.XtraBars.Docking2010.Views.BaseDocument doc in tabbedView1.Documents)
            {
                if (doc.Control.Name == control.Name)
                {
                    tabbedView1.Controller.Activate(doc);
                    return true;
                }
            }
            return false;
        }
        public bool ExistTab(string caption)
        {
            foreach (DevExpress.XtraBars.Docking2010.Views.BaseDocument doc in tabbedView1.Documents)
            {
                if (doc.Caption == caption)
                {
                    tabbedView1.Controller.Activate(doc);
                    return true;
                }
            }
            return false;
        }
        public void AddTab(DevExpress.XtraEditors.XtraUserControl control, string caption)
        {
            if (ExistTab(caption)) return;

            tabbedView1.BeginUpdate();
            DevExpress.XtraBars.Docking2010.Views.BaseDocument document = tabbedView1.AddDocument(control);
            document.Control.Name = control.Name;
            document.Caption = caption;

            tabbedView1.EndUpdate();
            tabbedView1.Controller.Activate(document);
        }
        public void RemoveTab(DevExpress.XtraEditors.XtraUserControl control)
        {
            tabbedView1.BeginUpdate();
            tabbedView1.RemoveDocument(control);
            tabbedView1.EndUpdate();
        }
        public void CloseAllTabs()
        {
            tabbedView1.Controller.CloseAll();
        }
        private void tabbedView1_QueryControl(object sender, DevExpress.XtraBars.Docking2010.Views.QueryControlEventArgs e)
        {
            //if (e.Document.ControlName == "XtraUserControl1") 
            //e.Control = new XtraUserControl1();
            //else e.Control = new ucBranch();
        }
    }
}
